/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.el;

import java.beans.FeatureDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import javax.el.ValueExpression;
import javax.el.VariableMapper;

/**
 * Provides a simple context for using expression language outside of JSF.
 * 
 * @author boudyacho
 */
public class Context extends ELContext {

	private Map<String, Object> variables = new HashMap<String, Object>();

	private CompositeELResolver resolver;
	private VariableMapper variableMapper = new VariableMapperImpl();
	private FunctionMapper functionMapper = new FunctionMapperImpl();

	public Context() {
		resolver = new CompositeELResolver();
		resolver.add(new ELResolverImpl());
		resolver.add(new MapELResolver());
		resolver.add(new ListELResolver());
		resolver.add(new ArrayELResolver());
		// resolver.add(new ResourceBundleELResolver());
		resolver.add(new BeanELResolver());
	}

	public void put(String variable, Object obj) {
		variables.put(variable, obj);
	}

	@Override
	public ELResolver getELResolver() {
		return resolver;
	}

	@Override
	public VariableMapper getVariableMapper() {
		return variableMapper;
	}

	@Override
	public FunctionMapper getFunctionMapper() {
		return functionMapper;
	}

	// ---------------------------------------------------------------
	// impls
	// ---------------------------------------------------------------

	private class ELResolverImpl extends ELResolver {

		@Override
		public Object getValue(ELContext context, Object base, Object property) {
			if (base == null) {
				Object result = variables.get(property);
				if (result != null) {
					context.setPropertyResolved(true);
					return result;
				}
			}
			return null;
		}

		@Override
		public Class<?> getCommonPropertyType(ELContext arg0, Object arg1) {
			return null;
		}

		@Override
		public Iterator<FeatureDescriptor> getFeatureDescriptors(
				ELContext arg0, Object arg1) {
			return null;
		}

		@Override
		public Class<?> getType(ELContext arg0, Object arg1, Object arg2) {
			return null;
		}

		@Override
		public boolean isReadOnly(ELContext arg0, Object arg1, Object arg2) {
			return false;
		}

		@Override
		public void setValue(ELContext arg0, Object arg1, Object arg2,
				Object arg3) {
		}
	}

	private class VariableMapperImpl extends VariableMapper {
		public ValueExpression resolveVariable(String s) {
			return null;
		}

		public ValueExpression setVariable(String s,
				ValueExpression valueExpression) {
			return valueExpression;
		}
	}

	private class FunctionMapperImpl extends FunctionMapper {
		public Method resolveFunction(String s, String s1) {
			return null;
		}
	}
}
